package com.sias.realestate.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sias.realestate.web.model.Owner;
import com.sias.realestate.web.model.Tenement;
import com.sias.realestate.web.model.User;
import com.sias.realestate.web.service.OwnerService;
import com.sias.realestate.web.service.TenementService;
import com.sias.realestate.web.service.UserService;
import com.sias.realestate.web.util.FinalVarUtil;
import com.sias.realstate.web.pagemodel.PageModel;

@Controller
@RequestMapping(value = "/owner")
public class OwnerController {

	@Resource
	private UserService userService;
	@Resource
	private OwnerService ownerService;
	@Resource
	private TenementService tenementService;

	@RequestMapping(value = "/getAllOwner", method = RequestMethod.GET)
	public ModelAndView getAllOwner(HttpServletRequest request)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<Owner> list = ownerService.selectList();
		String th_list = FinalVarUtil.ownerTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("owner/ownerList");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：删除住户信息
	 */
	@RequestMapping(value = "/deleteOwner", method = RequestMethod.GET)
	public ModelAndView deleteOwner(HttpServletRequest request,Integer oid) {
		ModelAndView mv = new ModelAndView("redirect:getAllOwner");
		ownerService.delete(oid);
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：我的基本信息 创建日期：2016年12月13日下午5:41:11
	 */
	@RequestMapping(value = "/ownerInfo", method = RequestMethod.GET)
	public ModelAndView ownerInfo(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		User user = (User) request.getSession().getAttribute("user");
		Owner owner = ownerService.findByIdcard(user.getuIdcard());
		Tenement tenement;
		if (owner != null) {
			tenement = tenementService.findTenementByIdcard(owner.getoIdcard());
			mv.addObject("owner", owner);
			if (tenement != null) {
				mv.addObject("tenement", tenement);
			} else {
				mv.addObject("message1", "您没有房屋信息！");
			}
		} else {
			mv.addObject("message2", "您不是小区业主！");
			mv.addObject("message1", "您没有房屋信息！");
		}
		mv.addObject("user", user);
		mv.setViewName("owner/ownerInfo");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：查看业主详情 创建日期：2016年12月13日下午5:41:11
	 */
	@RequestMapping(value = "/ownerdetail", method = RequestMethod.GET)
	public ModelAndView ownerdetail(HttpServletRequest request, Integer oid) {
		ModelAndView mv = new ModelAndView();
		Owner owner = ownerService.selectById(oid);
		Tenement tenement = tenementService.findTenementByIdcard(owner.getoIdcard());
		mv.addObject("owner", owner);
		mv.addObject("tenement", tenement);
		mv.setViewName("owner/ownerdetail");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：跳转到添加新业主页面 创建日期：2016年12月16日上午11:41:19
	 */
	@RequestMapping(value = "/addOwnerJsp", method = RequestMethod.GET)
	public ModelAndView addOwnerJsp(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("owner/addowner");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：添加新业主 创建日期：2016年12月16日上午11:41:19
	 */
	@RequestMapping(value = "/addOwner", method = RequestMethod.POST)
	public ModelAndView addOwner(HttpServletRequest request, Owner owner) {
		ModelAndView mv = new ModelAndView();
		owner.setoStatus("0");
		ownerService.insert(owner);
		request.setAttribute("successInfo", "添加新住户成功！");
		request.setAttribute("page_code", "allowner");
		mv.setViewName("/success-info");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：进入办理入住页面 创建日期：2016年12月16日上午11:41:19
	 */
	@RequestMapping(value = "/checkInJsp", method = RequestMethod.GET)
	public ModelAndView checkInJsp(HttpServletRequest request, Integer oid) {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<Tenement> list = tenementService.findTenementByOidNull();
		String th_list = FinalVarUtil.checkintenementTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("oid", oid);
		mv.addObject("list", list);
		mv.setViewName("owner/checkinList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：办理入住 创建日期：2016年12月16日上午11:41:19
	 */
	@RequestMapping(value = "/checkIn", method = RequestMethod.GET)
	public ModelAndView checkIn(HttpServletRequest request, Integer oid, Integer tid) {
		ModelAndView mv = new ModelAndView();
		Owner owner = ownerService.selectById(oid);
		owner.setoStatus("1");
		owner.setoTime(new Date());
		ownerService.update(owner);
		String idcard = owner.getoIdcard();
		Tenement tenement = tenementService.selectById(tid);
		tenement.setoIdcard(idcard);
		tenementService.update(tenement);
		request.setAttribute("successInfo", "办理入住成功！");
		request.setAttribute("page_code", "allowner");
		mv.setViewName("/success-info");
		return mv;
	}

}
